<?php
function ejd($status='',$msg='',$href='',$data=array()){
	echo json_encode(array(
		'status'=>$status,
		'msg'=>$msg,
		'href'=>$href,
		'data'=>$data
	));
	exit;
}

function create_shopex_ac($arr_params) {
	$token = md5('shopexentuser');
    ksort($arr_params);
    $str = '';
    foreach( $arr_params as $key => $value )
    {
        if( $key != 'certi_ac' )
        {
            $str .= $value;
        }
    }
    return md5($str.$token);
}

function echoParam($pname){
	if(!empty($pname)) echo $pname;
}

function b64pattern() {
	
    return array(
     '+'=>'_1_',
     '/'=>'_2_',
     '='=>'_3_',
    );
    
}

function b64_encode($str) {
    $str = base64_encode($str);
    return strtr($str, b64pattern());
}

function b64_decode($str){
    $str = strtr($str, array_flip(b64pattern()));
    return base64_decode($str);
}

/**
	 +----------------------------------------------------------
 * 产生随机字串，可用来自动生成密码
 * 默认长度6位 字母和数字混合 支持中文
	 +----------------------------------------------------------
 * @param string $len 长度
 * @param string $type 字串类型
 * 0 字母 1 数字 其它 混合
 * @param string $addChars 额外字符
	 +----------------------------------------------------------
 * @return string
	 +----------------------------------------------------------
 */
function rand_string($len = 6, $type = '', $addChars = '') {
	$str = '';
	switch ($type) {
		case 0 :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
			break;
		case 1 :
			$chars = str_repeat ( '0123456789', 3 );
			break;
		case 2 :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
			break;
		case 3 :
			$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
			break;
		default :
			// 默认去掉了容易混淆的字符oOLl和数字01，要添加请使用addChars参数
			$chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
			break;
	}
	if ($len > 10) { //位数过长重复字符串一定次数
		$chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 );
	}
	if ($type != 4) {
		$chars = str_shuffle ( $chars );
		$str = substr ( $chars, 0, $len );
	} else {
		// 中文随机字
		for($i = 0; $i < $len; $i ++) {
			$str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 );
		}
	}
	return $str;
}


function rand_filename(){
	return date('Ymd-His-').rand_string(8);
}

function show_date($time){
	echo date('Y年m月d日 H时i分',$time);
}

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec)*10000;
}
        
function pr($p){
	echo '<pre>';
	print_r($p);
	exit;
}


function getCatTree($list, $pid = 0, &$t = array()){
	
	foreach ($list as $k=>$v){
		
		if($v['pid'] == $pid){
			
			$v['step'] = getCatDepth($list, $v['id']);

			$v['has_sub'] = hasCatSub($list, $v['id']);
			
			$t[] = $v;
			getCatTree($list,$v['id'],$t);
			
		}
	}
	
	return $t;
	
}

function getCatDepth($list,$id,&$i=0){
	
	foreach ($list as $v){
		if($v['id'] == $id && $v['pid'] != 0){
			$i++;
			getCatDepth($list,$v['pid'],$i);
		}
	}
	
	return $i;
	
}

function hasCatSub($list,$id){
	
	foreach ($list as $v){
		if($v['pid'] == $id){
			return true;
		}
	}

	return false;
}


function getCatFname($list,$id,$str_div = '&gt;'){
	$arr = getCatFnameArr($list, $id);
	
	foreach ($list as $k=>$v){
		if($v['id'] == $id) $arr[] = $v['title'];
	}
	
	$fname = implode($arr, $str_div);
	if(!empty($fname)) return $fname;
	return '其它';
}

function getCF($id,$str_div = '&gt;'){
	
	$Cat = D('WsCat');
	
	$list = $Cat->select();
	
	return getCatFname($list, $id,$str_div);
}

function getCatFnameArr($list,$id,&$t = array()){

	foreach ($list as $k=>$v){
		if($v['id'] == $id){
			
			$pid = $v['pid'];
			
			foreach ($list as $z=>$j){
				if($j['id'] == $pid){
					$t[] = $j['title'];
					getCatFnameArr($list, $j['id'],$t);
				}
			}
		
		}
		
	}
	
	return array_reverse($t);
	
}


function getCatSubId($list,$id,&$arr_sub_id= array()){
	
	foreach ($list as $k=>$v){
		
		if($v['pid'] == $id){
			
			$arr_sub_id[] = $v['id'];
			getCatSubId($list, $v['id'],$arr_sub_id);
			
		}
		
	}

	return array_merge(array($id),$arr_sub_id);
	
}

function getCatOpt($list,$astr = '|__'){
	$tree = getCatTree($list);
	$opt = array();
	foreach ($tree as $k=>$v){
		$str = '';
		for($i=0;$i<$v['step'];$i++){
			$str .= $astr;
		}
		$opt[$v['id']] = $str.$v['title'];
	}
	
	return $opt; 
	
}

function fd($vn,$dy='na',$ov = null){

	$v = '';
	$arr_vn = explode('.', $vn);
	$CI =& get_instance();
	$p = $CI->load->get_var($arr_vn[0]); 

	if(!empty($ov)) $p = $ov;

	if(empty($p)){
		$v = '';
	} elseif(!empty($p) && (!is_array($p) && !is_object($p))){
		$v = $p;
	}else{
		$p = (array)$p;
		for($i=1;$i<=count($arr_vn)-1;$i++){
			if(!empty($p[$arr_vn[$i]]) && (!is_array($p[$arr_vn[$i]]) && !is_object($p[$arr_vn[$i]]))){
				$p = $p[$arr_vn[$i]];
			}elseif(empty($p[$arr_vn[$i]])){
				$p = '';
			}else{
				$p = (array)$p[$arr_vn[$i]];
			}
		}
		$v = $p;
	}
	
	if($dy == 'na'){
		return $v;
	}elseif($dy == 'a'){
	    return (array)$v;
    }elseif($dy == 'i'){
		return intval($v);
    }elseif($dy == 'f'){
        $v = floatval($v);
		return round($v,2);
    }
	
}

function jsonString($str)
{
	return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}

function formatBytes($bytes) {
	if($bytes >= 1073741824) {
		$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
	} elseif($bytes >= 1048576) {
		$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
	} elseif($bytes >= 1024) {
		$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
	} else {
		$bytes = $bytes . 'Bytes';
	}
	return $bytes;
}

